#include "PBufArry.h"

void PBufArray_init(PBufArray *buf, unsigned char *array, unsigned int array_len) {
    buf->buf = array;
    buf->buf_len = array_len;
    buf->r_idx = 0;
    buf->w_idx = 0;
}

int PBufArray_wirite_byte(PBufArray *buf, unsigned char ch) {
    if (buf->w_idx < buf->buf_len) {
        buf->buf[buf->w_idx] = ch;
        buf->w_idx++;
        return PBUFARRAY_RET_SUCCEED;
    }
    return PBUFARRAY_RTE_ERRAR;
}

int PBufArray_read_byte(PBufArray *buf, unsigned char *ch) {
    if (buf->r_idx < buf->buf_len) {
        *ch = buf->buf[buf->r_idx];
        buf->r_idx++;
        return PBUFARRAY_RET_SUCCEED;
    }
    return PBUFARRAY_RTE_ERRAR;
}

int PBufArray_reset(PBufArray *buf) {
    buf->r_idx = 0;
    buf->w_idx = 0;
    return PBUFARRAY_RET_SUCCEED;
}

int PBufArray_read_back(PBufArray *buf) {
    if (buf->r_idx > 0) {
        buf->r_idx--;
        return PBUFARRAY_RET_SUCCEED;
    }
    return PBUFARRAY_RTE_ERRAR;
}

int PBufArray_wirte_back(PBufArray *buf) {
    if (buf->w_idx > 0) {
        buf->w_idx--;
        return PBUFARRAY_RET_SUCCEED;
    }
    return PBUFARRAY_RTE_ERRAR;
}
